package cn.nexd.app.collector.adapter;

import android.content.Context;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.TextView;

import java.util.List;

import cn.nexd.app.collector.R;
import cn.nexd.app.collector.model.FileManagerModel;

/**
 * Created by xunzongxia on 16/10/10.
 */

public class ErrorListAdapter extends BaseExpandableListAdapter implements ExpandableListAdapter {
    private static final int GROUP_ITEM_RESOURCE = R.layout.item_file_collect_date;
    private static final int CHILD_ITEM_RESOURCE = android.R.layout.simple_list_item_1;
    private LayoutInflater layoutInflater;
    private List<FileManagerModel> fileManagerModels;
    private Context context;

    public ErrorListAdapter(Context context, List<FileManagerModel> fileManagerModels) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
        this.fileManagerModels = fileManagerModels;

    }

    @Override
    public int getGroupCount() {
        return fileManagerModels.size();
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        return fileManagerModels.get(groupPosition).getChildFileNames().size();
    }

    @Override
    public Object getGroup(int groupPosition) {
        return fileManagerModels.get(groupPosition);
    }

    @Override
    public Object getChild(int groupPosition, int childPosition) {
        return fileManagerModels.get(groupPosition).getChildFileNames().get(childPosition);
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public boolean hasStableIds() {
        return true;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ParentViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = layoutInflater.inflate(GROUP_ITEM_RESOURCE, parent, false);
            viewHolder = new ParentViewHolder(convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ParentViewHolder) convertView.getTag();
        }
        viewHolder.tvDate.setText(fileManagerModels.get(groupPosition).getParentName());

        return convertView;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
        TextView textView = getTextView();
        textView.setMaxLines(2);
        textView.setText(fileManagerModels.get(groupPosition).getChildFileNames().get(childPosition));
        return textView;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }

    private TextView getTextView() {
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 120);
        TextView textView = new TextView(context);
        textView.setLayoutParams(lp);
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        textView.setPadding(40, 0, 0, 0);
        textView.setTextSize(20);
        return textView;
    }

    public class ChildViewHolder {
        private TextView tvName;

        public ChildViewHolder(View convertView) {
            tvName = (TextView) convertView.findViewById(android.R.id.text1);
        }
    }

    public class ParentViewHolder {
        private TextView tvDate;

        public ParentViewHolder(View convertView) {
            tvDate = (TextView) convertView.findViewById(R.id.tv_name);
        }
    }
}
